#include "widget.h"
#include "./ui_widget.h"

#include <QGraphicsDropShadowEffect>

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    initView();
}

Widget::~Widget()
{
    delete ui;
}

/**
 * 初始化视图
 */
void Widget::initView()
{
    // 初始隐藏所有面板
    ui->functionPanels->setVisible(false);

    // 为所有卡片添加阴影
    applyCardShadows();

    // 卡片内的子控件忽略鼠标事件
    ui->label_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_3->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_2_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_2_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_2_3->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_3_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_3_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_3_3->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_4_1->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_4_2->setAttribute(Qt::WA_TransparentForMouseEvents, true);
    ui->label_4_3->setAttribute(Qt::WA_TransparentForMouseEvents, true);

    // 连接信号槽
    connect(ui->btnElectricStim, &QPushButton::clicked, this, &Widget::onElectricalStimClicked);
    connect(ui->btnEmgFeedback, &QPushButton::clicked, this, &Widget::onEmgFeedbackClicked);
    connect(ui->btnMuscleAssess, &QPushButton::clicked, this, &Widget::onMuscleAssessClicked);
    connect(ui->btnGripRelease, &QPushButton::clicked, this, &Widget::onGripReleaseClicked);
    connect(ui->btnShutdown, &QPushButton::clicked, this, &Widget::showShutdownDialog);
}

/**
 * 给图片添加阴影
 */
void Widget::applyCardShadows()
{
    // 获取所有的卡片
    QList<QPushButton*> cards = {
        ui->btnElectricStim,
        ui->btnEmgFeedback,
        ui->btnMuscleAssess,
        ui->btnGripRelease
    };

    // 循环设置阴影效果
    for(QPushButton* card : cards) {
        QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
        shadow->setBlurRadius(15);
        shadow->setOffset(0,3);
        shadow->setColor(QColor(0,0,0,60));
        card->setGraphicsEffect(shadow);

        card->setStyleSheet(
            "QPushButton {"
            "    border-radius: 20px;"
            "    background: qlineargradient("
            "        x1:0, y1:0, x2:0, y2:1,"
            "        stop:0 #F0FAFF, stop:1 #EEEEEE"
            "        );"
            "border: 1px solid #E0E0E0;"
            "}"
            "QPushButton:hover {"
            "   background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #ECF0F2, stop:1 #EEEEEE);"
            "   border: 1px solid #D0D0D0;"
            "}"
        );
    }
}

/**
 * 点击功能电刺激
 */
void Widget::onElectricalStimClicked()
{
    ui->functionPanels->setCurrentIndex(0);
    ui->functionPanels->setVisible(true);
}

/**
 * 点击肌电反馈
 */
void Widget::onEmgFeedbackClicked()
{
    ui->functionPanels->setCurrentIndex(3);
    ui->functionPanels->setVisible(true);
}

/**
 * 点击肌力评估
 */
void Widget::onMuscleAssessClicked()
{
    ui->functionPanels->setCurrentIndex(1);
    ui->functionPanels->setVisible(true);
}

/**
 * 点击抓握释放
 */
void Widget::onGripReleaseClicked()
{
    ui->functionPanels->setCurrentIndex(2);
    ui->functionPanels->setVisible(true);
}

/**
 * 显示关机的弹窗
 */
void Widget::showShutdownDialog()
{
    // 1. 创建对话框对象
    shutdownDialog = new QDialog(this);

    // 2. 创建UI对象，并设置到对话框上
    shutdowmUi = new Ui::ShutdownDialog();
    shutdowmUi->setupUi(shutdownDialog);

    // 4. 显示模态对话框
    shutdownDialog->exec();
}
